﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Navigation;
using System.Device.Location;
using Microsoft.Phone.Controls.Maps;
using ProtoType.GeocodeService;

namespace ProtoType
{
    public partial class AddressMap : PhoneApplicationPage
    {
        IGeoPositionWatcher<GeoCoordinate> watcher;
        string accuracyText = "";

        public AddressMap()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            string address = "";
            base.OnNavigatedTo(e);
            if (NavigationContext.QueryString.TryGetValue("addr", out address))
            {
                GetLattAndLong(address);
            }
        }

        private void GetLattAndLong(string address)
        {
            GeocodeRequest geocodeRequest = new GeocodeRequest();
            //Set the credentials using a valid Bing Maps key
            RequestBase req = new RequestBase();
            geocodeRequest.Credentials = new ProtoType.GeocodeService.Credentials();
            geocodeRequest.Credentials.ApplicationId = "Any8oa_tRDKqhncXvK3mEAKnUZXzONZsuu46NLOYee1nMoEsr_dwQ44VO62sV2Nd";
            // Set the full address query
            geocodeRequest.Query = address;
            // Set the options to only return high confidence results
            ConfidenceFilter[] filters = new ConfidenceFilter[1];
            filters[0] = new ConfidenceFilter();
            filters[0].MinimumConfidence = GeocodeService.Confidence.High;
            GeocodeOptions geocodeOptions = new GeocodeOptions();
            //geocodeOptions.Filters = filters;
            geocodeRequest.Options = geocodeOptions;
            GeocodeServiceClient geocodeService = new GeocodeServiceClient("BasicHttpBinding_IGeocodeService");
            geocodeService.GeocodeCompleted += new EventHandler<GeocodeCompletedEventArgs>(geocodeService_GeocodeCompleted);
            geocodeService.GeocodeAsync(geocodeRequest);
        }

        private readonly Microsoft.Phone.Controls.Maps.CredentialsProvider _credentialsProvider;

        public Microsoft.Phone.Controls.Maps.CredentialsProvider CredentialsProvider
        {
            get { return _credentialsProvider; }
        }

        void geocodeService_GeocodeCompleted(object sender, GeocodeCompletedEventArgs e)
        {
            GeocodeResponse geocodeResponse = e.Result;
            double latitude = geocodeResponse.Results[0].Locations[0].Latitude;
            double longitude = geocodeResponse.Results[0].Locations[0].Longitude;

            //Microsoft.Phone.Controls.Maps.Map MyMap = new Microsoft.Phone.Controls.Maps.Map();
            //MyMap.CredentialsProvider = CredentialsProvider;
            //MyMap.Height = 250;
            //MyMap.Name = "MyMap";
            //MyMap.Mode = new Microsoft.Phone.Controls.Maps.AerialMode(true);
            //ContentPanel.Children.Add(MyMap);
        }
    }
}